<template>
  <div>
    <el-dialog title="分配角色" :visible="showRole" @close="btncancel">
      <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
      <el-checkbox-group v-model="checkedList">
        <!-- 选项 -->
        <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
      </el-checkbox-group>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnOk">确定</el-button>
          <el-button size="small" @click="btncancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserById, assignRoles } from '@/api/user'
export default {
  props: {
    showRole: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      roleList: [],
      checkedList: [],
      userId: ''
    }
  },
  async created() {
    //   获取角色列表
    const { rows } = await getRoleList()
    // 保存角色列表渲染
    this.roleList = rows
  },
  methods: {
    async getRoleList(id) {
      // 用户基本信息
      const { roleIds } = await getUserById(id)
      //   数据回显
      this.checkedList = roleIds
      this.userId = id
    //  // console.log(roleIds)
    },
    // 确定
    async btnOk() {
      // 发起分配角色请求
      await assignRoles({
        id: this.userId,
        roleIds: this.checkedList
      })
      //   提示用户
      this.$message.success('分配成功')
      //   关闭弹窗
      this.$parent.showRole = false
    },
    // 离开
    btncancel() {
      // 关闭弹窗
      this.$parent.showRole = false
    }
  }
}
</script>

<style>

</style>
